package com.magicsoft.share.customview.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.magicsoft.share.R;

/**
 * @Author hwg
 * @Description：分享弹窗
 * @Date 2018/3/26
 */
public class DialogShare extends DialogFragment {

    private ImageView mIvFriendCircle, mIvWechatFriend ;
    private TextView mTvCancel ;

    private OnButtonClickListener mOnButtonClickListener ;

    public static DialogShare newInstance(){
        DialogShare dialogShare = new DialogShare() ;
        return dialogShare ;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE) ;
        getDialog().getWindow().getAttributes().windowAnimations = R.style.style_push_down_anim ;

        View view = inflater.inflate(R.layout.dialog_share, container, false) ;
        initView(view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow() ;
        // 一定要设置Background，如果不设置，window属性设置无效
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        window.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams layoutParams = window.getAttributes() ;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT ;
        window.setAttributes(layoutParams);

    }

    private void initView(View view){
        mIvFriendCircle = view.findViewById(R.id.iv_wechat_friend_circle_share) ;
        mIvWechatFriend = view.findViewById(R.id.iv_wechat_friend_share) ;
        mTvCancel = view.findViewById(R.id.tv_cancel) ;

        mIvFriendCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnButtonClickListener != null){
                    mOnButtonClickListener.shareToFriendCircle();
                }
                dismiss();
            }
        });

        mIvWechatFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnButtonClickListener != null){
                    mOnButtonClickListener.shareToWechatFriend();
                }
                dismiss();
            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public interface OnButtonClickListener{
        void shareToWechatFriend();
        void shareToFriendCircle();
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener){
        this.mOnButtonClickListener = onButtonClickListener ;
    }

}
